import tools from '@/tools/tools.js'
import CryptoJS from 'crypto-js'
import {
	API_URL,
	APP_TYPE
} from '@/env.js'
import api from '@/api/api.js'
//判断
function isNext(needToken, header) {
	if (needToken) {
		//用户的token
		let token = uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
		if (token) {
			header["token"] = token
			return true
		} else {
			tools.showtt('请登录后操作')
			return false
		}
	} else {
		return true
	}
}

//获取接口签名
const getInterfaceSign = function() {
	let timestamp = new Date().getTime();
	return {
		timestamp: timestamp,
		sign: CryptoJS.MD5(`${timestamp}df09e90c4144`).toString()
	};
}

//接口请求
export function aixos(url, method, params, needToken) {
	return new Promise((resolve, reject) => {
		tools.showload('')
		let header = {
			...getInterfaceSign(),
		}
		if (isNext(needToken, header)) {
			uni.request({
				url: API_URL + url,
				method: method,
				header: header,
				data: {
					...params
				},
				success(res) {
					if (res.data.code === 200) {
						resolve(res.data);
					} else if (res.data.code === 456) {
						//重新登录
						anewRequest(url, method, params, needToken)
					} else {
						tools.showtt(res.data.msg)
						reject(res.data.msg);
					}

				},
				fail(err) {
					reject(err.errMsg);
					tools.showtt('请求失败')
				},
				complete() {
					uni.hideLoading();
				}
			});
		}
	});
};
//静默登录
export function silentLogin(url, method, provider) {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: provider,
			success: (e) => {
				console.log("获取登录信息：", e);
				let loginRes = {
					code: e.code,
					platform: APP_TYPE
				}
				aixos(url, method, loginRes, false).then((res) => {
					resolve(res)
				}, (rej) => {
					reject(rej)
				})
			},
			fail: (err) => {
				tools.showtt('登录调用失败')
				reject(rej)
				console.log(err);
			},
			complete: (e) => {
				console.log(e);
			}
		});
	});
}
//授权登录
export function authorizationLogin(provider, url, method, platform) {
	return new Promise((resolve, reject) => {
		uni.getUserInfo({
			provider: provider,
			success: function(e) {
				console.log("获取授权登录信息：", e);
				let infoRes = {
					img: e.userInfo.avatarUrl,
					userName: e.userInfo.nickName,
					platform: platform,
				}
				aixos(url, method, infoRes, true).then((res) => {
					resolve(res)
				}, (rej) => {
					reject(rej)
				})
			},
			fail: (err) => {
				tools.showtt('登录调用失败')
				reject(err)
				console.log(err);
			},
			complete: (e) => {
				console.log(e);
			}
		});
	});
}
//获取手机号
export function getPhoneNumber(e, url, method, platform) {
	return new Promise((resolve, reject) => {
		let infoRes = {
			code: e.detail.code,
			platform: platform
		}
		aixos(url, method, infoRes, true).then((res) => {
			resolve(res)
			tools.showtt('获取成功')
		}, (rej) => {
			reject(rej)
		})
	});
}
//重新获取Token并重新发起当前请求
export function anewRequest(url, method, params, needToken) {
	api.silentLogin().then(res => {
		uni.setStorageSync('token', res.data.token)
		aixos(url, method, params, needToken)
	})
}
//生成支付参数并拉起支付
export function createParmas(url, method, orderId, paytype) {
	return new Promise((resolve, reject) => {
		let parmas = {
			goodsId: orderId
		}
		aixos(url, method, parmas, true).then(res => {
			// resolve(res)
			payFn(paytype, res.data).then(res => {
				resolve(res)
			})
		}, rej => {
			reject(rej)
		})
	})
};
//拉起支付
export function payFn(provider, parmas) {
	console.log(parmas, '支付参数')
	return new Promise((resolve, reject) => {
		uni.requestPayment({
			provider,
			...parmas,
			orderInfo: parmas,
			success: (res) => {
				tools.showtt('支付成功', 'success')
				resolve(res)
			},
			fail: (err) => {
				tools.showtt('支付取消', 'error')
				reject(err)
			}
		});
	})
};
